package com.tour.controller;

import com.tour.bean.*;
import com.tour.service.HotelService;
import com.tour.service.UserHotelService;
import com.tour.service.UserMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * hotel
 * @author
 * @date 2020/03/16
 */
@RestController
@RequestMapping(value = "/Hotel")
public class HotelController {

    @Autowired
    private HotelService hotelService;
    @Autowired
    private UserHotelService userHotelService;
    @Autowired
    private UserMessageService userMessageService;

    /**
     * [查询] 根据主键 id 查询
     * @author
     * @date 2020/03/16
     **/
    @RequestMapping("/load/{id}")
    public Object load(@PathVariable("id") int id){
        return hotelService.load(id);
    }

    /**
     * [查询] 分页查询
     * @author
     * @date 2020/03/16
     **/
    @RequestMapping("/pageList")
    public Map<String, Object> pageList(@RequestBody Map<String,Object> map) {
        int offset= (int) map.get("offset");
        int pagesize= (int) map.get("pagesize");
        String title="";
        if(map.containsKey("title")){
            title= (String) map.get("title");
        }
        String address="";
        if(map.containsKey("address")){
            address= (String) map.get("address");
        }
        return hotelService.pageList(offset, pagesize,title,address);
    }

    /**
     * [新增] 用户订购酒店
     * @author
     * @date 2020/03/16
     **/
    @RequestMapping("/insert/{id}")
    public ReturnT<String> insert(@PathVariable("id") int id, HttpServletRequest request){
        Consumer userInfo = (Consumer) request.getSession().getAttribute("userInfo");
        if(null==userInfo){
            return new ReturnT<>(401, "请先登录！");
        }
        UserHotel byid = userHotelService.getByIdId(userInfo.getId(), id);
        if(null!=byid){
            return new ReturnT<>(402, "已经存在！");
        }
        UserHotel uh=new UserHotel(userInfo.getId(), id, new Date());
        userHotelService.insert(uh);
        return ReturnT.SUCCESS;
    }

    /**
     * 加载留言
     * @param id
     * @return
     */
    @RequestMapping("/loadMsg/{id}")
    public ReturnT<List<Map>> loadMsg(@PathVariable("id") int id){
        List<Map<String, Object>> mapList = userMessageService.getMsgByModel("H", id);
        ReturnT<List<Map>> result=new ReturnT(mapList);
        return result;
    }

    /**
     * [新增] 用户留言
     * @author
     * @date 2020/03/18
     **/
    @RequestMapping("/insertMsg")
    public ReturnT<String> insertMsg(@RequestBody Map<String,String> map, HttpServletRequest request){
        Consumer userInfo = (Consumer) request.getSession().getAttribute("userInfo");
        if(null==userInfo){
            return new ReturnT<>(401, "请先登录！");
        }
        if (map.containsKey("id")&&map.containsKey("message")) {
            String message=map.get("message");
            if (StringUtils.isEmpty(message)){
                return new ReturnT<>(402, "缺失必要参数");
            }
            UserMessage msg=new UserMessage();
            msg.setModelId(Integer.parseInt(map.get("id")));
            msg.setMsgModel("H");
            msg.setCurrTime(new Date());
            msg.setUserId(userInfo.getId());
            msg.setMsgContent(message);
            msg.setIsReply("0");
            userMessageService.insert(msg);
        }else {
            return new ReturnT<>(402, "缺失必要参数");
        }

        return ReturnT.SUCCESS;
    }



}
